<?php
session_start ();
$_SESSION ['pathway'] = '../..';
require_once ($_SESSION ['pathway'] . "/commun/ouvre_base.php");
// ~ $link = mysqli_connect($host, $userbase, $passwd) or die('Impossible de se connecter pour récupérer niveau : ' . mysql_error());
mysql_select_db ( $db ) or die ( 'Impossible de s&#233;lectionner la base de donn&#233;es' );
// ~ mysqli_select_db($link,$db) or die('Impossible de s&#233;lectionner la base de donn&#233;es');

// ~ $edit = $_SESSION['edit'];

/*
 * $liste_fichiersattaches = array();
 * //echo getcwd();
 * if(isset($_FILES['fichiersattaches']) AND $_FILES['fichiersattaches']['error'] == 0){
 * move_uploaded_file($_FILES['fichiersattaches']['tmp_name'], $_SESSION['pathway'].$_SESSION['pathway_js'].'/uploads/'. basename($_FILES['fichiersattaches']['name']));
 * chmod($_SESSION['pathway'].$_SESSION['pathway_js'].'/uploads/' . basename($_FILES['fichiersattaches']['name']), 0777);
 * $fichiersattaches .= basename($_FILES['fichiersattaches']['name']).";";
 * }
 *
 * if(isset($_POST['fichiersattachesoriginal_0'])){
 * $fichiersattaches .= $_POST['fichiersattachesoriginal_0'];
 * }
 */

if (isset ( $_POST ['date'] ) and $_POST ['date'] != '') {
	$date = $_POST ['date'];
} else {
	$date = '';
}
if (isset ( $_POST ['nip'] ) and $_POST ['nip'] != '') {
	$nip = $_POST ['nip'];
} else {
	$nip = '';
}
if (isset ( $_POST ['user'] ) and $_POST ['user'] != '') {
	$user = $_POST ['user'];
} else {
	$user = '';
}
if (isset ( $_POST ['last_modifier'] ) and $_POST ['last_modifier'] != '') {
	$last_modifier = strtoupper ( $_POST ['last_modifier'] );
} else {
	$last_modifier = '';
}
if (isset ( $_POST ['neurologist'] ) and $_POST ['neurologist'] != '') {
	$neurologist = strtoupper ( $_POST ['neurologist'] );
} else {
	$neurologist = '';
}
if (isset ( $_POST ['site'] ) and $_POST ['site'] != '') {
	$site = $_POST ['site'];
} else {
	$site = '';
}
if (isset ( $_POST ['sex'] ) and $_POST ['sex'] != '') {
	$sex = $_POST ['sex'];
} else {
	$sex = '';
}
if (isset ( $_POST ['birthdate'] ) and $_POST ['birthdate'] != '') {
	$birthdate = $_POST ['birthdate'];
} else {
	$birthdate = '';
}
if (isset ( $_POST ['location'] ) and $_POST ['location'] != '') {
	$location = $_POST ['location'];
} else {
	$location = '';
}
if (isset ( $_POST ['location_other'] ) and $_POST ['location_other'] != '') {
	$location_other = $_POST ['location_other'];
} else {
	$location_other = '';
}
if (isset ( $_POST ['diagnostic'] ) and $_POST ['diagnostic'] != '') {
	$diagnostic = $_POST ['diagnostic'];
	if (is_array ( $_POST ['diagnostic'] )) {
		$diagnostic = implode ( ', ', $_POST ['diagnostic'] );
	}
} else {
	$diagnostic = '';
}
if (isset ( $_POST ['diagnostic_genetique'] ) and $_POST ['diagnostic_genetique'] != '') {
	$diagnostic_genetique = $_POST ['diagnostic_genetique'];
} else {
	$diagnostic_genetique = '';
}
if (isset ( $_POST ['diagnostic_other'] ) and $_POST ['diagnostic_other'] != '') {
	$diagnostic_other = $_POST ['diagnostic_other'];
} else {
	$diagnostic_other = '';
}
if (isset ( $_POST ['diagnostic_genetique_specify'] ) and $_POST ['diagnostic_genetique_specify'] != '') {
	$diagnostic_genetique_specify = $_POST ['diagnostic_genetique_specify'];
	$diagnostic_genetique_specify = mysql_real_escape_string ( $diagnostic_genetique_specify );
} else {
	$diagnostic_genetique_specify = '';
}
if (isset ( $_POST ['diagnostic_complet'] ) and $_POST ['diagnostic_complet'] != '') {
	$diagnostic_complet = $_POST ['diagnostic_complet'];
} else {
	$diagnostic_complet = '';
}
if (isset ( $_POST ['mode'] ) and $_POST ['mode'] != '') {
	$mode = $_POST ['mode'];
	if (is_array ( $_POST ['diagnostic'] )) {
		$mode = implode ( ', ', $_POST ['mode'] );
	}
} else {
	$mode = '';
}
if (isset ( $_POST ['mode_other'] ) and $_POST ['mode_other'] != '') {
	$mode_other = $_POST ['mode_other'];
} else {
	$mode_other = '';
}
if (isset ( $_POST ['nouveau_patient'] ) and $_POST ['nouveau_patient'] != '') {
	$nouveau_patient = $_POST ['nouveau_patient'];
} else {
	$nouveau_patient = '';
}
if (isset ( $_POST ['prise_en_charge'] ) and $_POST ['prise_en_charge'] != '') {
	$prise_en_charge = $_POST ['prise_en_charge'];
} else {
	$prise_en_charge = '';
}
if (isset ( $_POST ['contexte_prise_en_charge'] ) and $_POST ['contexte_prise_en_charge'] != '') {
	$contexte_prise_en_charge = $_POST ['contexte_prise_en_charge'];
} else {
	$contexte_prise_en_charge = '';
} // cas ou l'on vient du modif_index
if (isset ( $_POST ['orientation_personnalisee'] ) and $_POST ['orientation_personnalisee'] != '') {
	$orientation_personnalisee = $_POST ['orientation_personnalisee'];
	if (is_array ( $_POST ['orientation_personnalisee'] )) {
		$orientation_personnalisee = implode ( ', ', $_POST ['orientation_personnalisee'] );
	}
} else {
	$orientation_personnalisee = '';
}
if (isset ( $_POST ['orientation_personnalisee_other'] ) and $_POST ['orientation_personnalisee_other'] != '') {
	$orientation_personnalisee_other = $_POST ['orientation_personnalisee_other'];
} else {
	$orientation_personnalisee_other = '';
}

if (isset ( $_GET ['action'] ) and $_GET ['action'] == 'edit') {
	$edit = $_GET ['index'];
	$update = "UPDATE `$db`.`fiche_activite` SET 
	`date` = '$date',
	`nip` = '$nip',
	`User` = '$user',
	`neurologist` = '$neurologist',
	`site` = '$site',
	`sex` = '$sex',
	`birthdate` = '$birthdate',
	`location` = '$location',
	`location_other` = '$location_other',
	`diagnostic` = '$diagnostic',
	`diagnostic_genetique` = '$diagnostic_genetique',
	`diagnostic_other` = '$diagnostic_other',
	`diagnostic_genetique_specify` = '$diagnostic_genetique_specify',
	`diagnostic_complet` = '$diagnostic_complet',
	`mode` = '$mode',
	`mode_other` = '$mode_other',
	`nouveau_patient` = '$nouveau_patient',
	`prise_en_charge` = '$prise_en_charge',
	`contexte_prise_en_charge` = '$contexte_prise_en_charge',
	`orientation_personnalisee` = '$orientation_personnalisee',
	`orientation_personnalisee_other` = '$orientation_personnalisee_other' 
	WHERE `fiche_activite`.`index` = '$edit';";
	// ~ echo $update;
	mysql_query ( $update ) or die ( "Can't modify form activite : " . mysql_error () );
	// ~ mysqli_query($link,$update) or die("Can't modify form activite : " . mysql_error());
	$_POST = array ();
} elseif (isset ( $_GET ['action'] ) and $_GET ['action'] == 'insert') {
	$recupereindexmaxfa = "select MAX(`index`) FROM `fiche_activite`";
	// ~ echo $recupereindexmaxfa;
	$resultindex = mysql_query ( $recupereindexmaxfa ) or die ( "Impossible de r&eacute;cup&eacute;rer l'index de la table fiche_activite" );
	// ~ $resultindex = mysqli_query($link,$recupereindexmaxfa) or die ("Impossible de r&eacute;cup&eacute;rer l'index de la table fiche_activite");
	while ( $ligne = mysql_fetch_row ( $resultindex ) ) {
		// ~ while ($ligne = mysqli_fetch_row($resultindex)) {
		$index_fa = $ligne [0] + 1;
	}
	$insertfa = "INSERT INTO `$db`.`fiche_activite` (
	`index`, 
	`date`, 
	`nip`, 
	`User`, 
	`neurologist`, 
	`site`, 
	`sex`, 
	`birthdate`, 
	`location`, 
	`location_other`, 
	`diagnostic`, 
	`diagnostic_genetique`, 
	`diagnostic_other`, 
	`diagnostic_genetique_specify`, 
	`diagnostic_complet`,
	`mode`, 
	`mode_other`, 
	`nouveau_patient`, 
	`prise_en_charge`, 
	`contexte_prise_en_charge`, 
	`orientation_personnalisee`,
	`orientation_personnalisee_other`
	) VALUES (
	'$index_fa',
	'$date',
	'$nip', 
	'$user',
	'$neurologist',
	'$site',
	'$sex',
	'$birthdate', 
	'$location', 
	'$location_other', 
	'$diagnostic', 
	'$diagnostic_genetique', 
	'$diagnostic_other', 
	'$diagnostic_genetique_specify', 
	'$diagnostic_complet',
	'$mode', 
	'$mode_other', 
	'$nouveau_patient', 
	'$prise_en_charge', 
	'$contexte_prise_en_charge', 
	'$orientation_personnalisee',
	'$orientation_personnalisee_other'
	);";
	mysql_query ( $insertfa ) or die ( "Impossible d'ins&eacute;rer donn&eacute;es dans table form activite : " . mysql_error () );
	// ~ mysqli_query($link,$insertfa) or die("Impossible d'ins&eacute;rer donn&eacute;es dans table form activite : " . mysql_error());
	// ~ echo $insertlinkage;
}

// ~ if(isset($_POST['submitprinc']) and $_POST['submitprinc'] == 'upload'){
// ~ $_POST = array();
// ~ echo "<script>window.location.href=\"../fiche_.php?action=edit&index=".$index."&table=linkage\"</script>";
// ~ }else
// ~ if(isset($_POST['submitprinc']) and $_POST['submitprinc'] == 'Save'){
$_POST = array ();
echo "<script>window.location.href=\"./../../start.php\"</script>";
// ~ }else{
// ~ $_POST = array();
// ~ echo "<script>window.location.href=\"../linkage.php?action=edit&index=".$index."&table=linkage\"</script>";
// ~ }

?>

